<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 添加按钮 -->
      <el-row :gutter="80">
        <el-col :span="4">
          <el-button type="primary" @click="showAddCategoryDialog"  style="margin-bottom: 15px;">添加分类</el-button>
        </el-col>
      </el-row>
      <el-row>
        <!-- 取消了展开和多选效果 -->
        <tree-table index-text="#" border show-index :expand-type="false" :selection-type="false" :data="categoryList" :columns="columns">
          <!-- 使用插槽展示boolean效果 -->
          <template slot="isok" slot-scope="scope">
            <i style="color: lightgreen;" v-if="!scope.row.cat_delete" class="el-icon-success"></i>
            <i style="color: red;" v-if="scope.row.cat_delete" class="el-icon-error"></i>
          </template>
          <template slot="level" slot-scope="scope">
            <el-tag type="primary" v-if="scope.row.cat_level === 0">一级</el-tag>
            <el-tag type="success" v-if="scope.row.cat_level === 1">二级</el-tag>
            <el-tag type="warning" v-if="scope.row.cat_level === 2">三级</el-tag>
          </template>
          <template slot="operation" slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.id)">编辑</el-button>
              <el-button type="danger" @click="deleRole(scope.row.id)" icon="el-icon-delete" size="mini">删除</el-button>
          </template>
        </tree-table>
      </el-row>
      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>
    <el-dialog @close="resetAdd" title="添加分类" :visible.sync="showAddDialog" width="50%">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <!-- prop用于输入校验 -->
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
          <div class="block">
            <!-- props指定配置对象 -->
            <el-cascader clearable :props="cascaderProps" v-model="selectedOption"
            :options="parentCateList" @change="parentCategoryChanged"></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddDialog=false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      addForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      addFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 2, max: 10, message: '名称长度在2到10个字符之间', trigger: 'blur' }
        ]
      },
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        expandTrigger: 'hover'
      },
      showAddDialog: false,
      categoryList: [],
      parentCateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      columns: [{
        label: '分类名称',
        prop: 'cat_name'
      }, {
        label: '是否有效',
        type: 'template',
        template: 'isok'
      }, {
        label: '排序',
        type: 'template',
        template: 'level'
      }, {
        label: '操作',
        type: 'template',
        template: 'operation'
      }],
      selectedOption: []
    }
  },
  created () {
    this.getCategories()
  },
  methods: {
    // 关闭对话框重置输入
    resetAdd () {
      this.$refs.addFormRef.resetFields()
    },
    showAddCategoryDialog () {
      this.getParentCateList()
      this.showAddDialog = true
    },
    async addCategory () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        console.log(this.selectedOption)
        if (this.selectedOption.length === 1) {
          this.addForm.cat_level = 1
          this.addForm.cat_pid = this.selectedOption[0]
        } else if (this.selectedOption.length === 2) {
          this.addForm.cat_level = 2
          this.addForm.cat_pid = this.selectedOption[1]
        } else {
          this.addForm.cat_level = 0
        }
        const { data: res } = await this.$http.post('/categories', this.addForm)
        if (res.meta.status !== 201) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.showAddDialog = false
        this.getCategories()
      })
    },
    async getParentCateList () {
      const { data: res } = await this.$http.get('/categories', { params: { type: 2 } })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.parentCateList = res.data
      console.log(this.parentCateList)
    },
    async getCategories () {
      const { data: res } = await this.$http.get('/categories', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.categoryList = res.data.result
      this.total = res.data.total
    },
    // 监听pagesize改变
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      // 注意改变后要重新获取列表
      this.getCategories()
    },
    // 监听页码值改变
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getCategories()
    },
    parentCategoryChanged () {
      // console.log(this.selectedOption)
    }
  }
}
</script>

<style lang='less' scoped>

</style>
